/*服务端*/

#include "head.h"
#include "operate.h"

/*ls指令，成功返回0，失败返回-1*/
int op_ls(Client *cli){
    //todo
    char buf[OP_BUF_SIZE] = {0};
    sprintf(buf, "ls %s", cli->cmd.cmd_arg);

    FILE *fp = popen(buf, "r");
    memset(buf, 0, sizeof(buf));

    int ret = fread(buf,sizeof(char), sizeof(buf), fp);
    if(ret == -1){
        //logError
        printf("ret == -1\n");
        return -1;
    }else if(ret == 0){
        //logWarning
        return -1;
    }else{
        memset(&cli->cmd, 0, sizeof(cli->cmd));
        memset(&cli->file, 0, sizeof(cli->file));
        cli->file.file_size = ret;
        /*发送结构体*/
        send(cli->user.fd_net, cli, sizeof(*cli), MSG_NOSIGNAL);
        /*发送数据*/
        send(cli->user.fd_net, buf, ret, MSG_NOSIGNAL);
        fclose(fp);
        return 0;
    }
}
